package com.yueshop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.yueshop.product.entity.AttrEntity;
import com.yueshop.product.service.AttrAttrgroupRelationService;
import com.yueshop.product.service.AttrService;
import com.yueshop.product.service.CategoryService;
import com.yueshop.product.vo.AttrGroupRelationVo;
import com.yueshop.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yueshop.product.entity.AttrGroupEntity;
import com.yueshop.product.service.AttrGroupService;
import com.yueshop.common.utils.PageUtils;
import com.yueshop.common.utils.R;



/**
 * 属性分组
 *
 * @author cr
 * @email cr@gmail.com
 * @date 2021-04-09 09:53:24
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    ///product/attrgroup/${this.spu.catalogId}/withattr

    @GetMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catalogId") Long catalogId){
        //1.查询出当前分类下的所有属性分组
        //2.查询出每个属性分组的所有属性
       List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatalogId(catalogId);
        return R.ok().put("data",vos);
    }




    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }







    ///product/attrgroup/" + this.attrGroupId + "/noattr/relation
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,@RequestParam Map<String, Object> params){
       PageUtils page =   attrService.getNoRelationAttr(attrGroupId,params);

        return R.ok().put("page",page);
    }





    //删除关联方法
    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){

        attrService.deleteRelation(vos);
        return R.ok();
    }




    ///product/attrgroup/" + this.attrGroupId + "/attr/relation
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation (@PathVariable("attrGroupId") Long attrGroupId){
      List<AttrEntity> entities =  attrService.getRelationAttr(attrGroupId);
        return  R.ok().put("data",entities);
    }



    /**
     * 列表
     */
    @RequestMapping("/list/{catrgoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catrgoryId") Long catrgoryId){
       //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catrgoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //获取三级分类id
        Long catelogId = attrGroup.getCatelogId();
        //根据三级分类id查询它的 父子孙 对应数据 ,并设置到AttrGroupEntity.catelogPath 属性中
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
